<?php
/*
 * cgi目录
 */
class CgiClassAction extends CommonAction{

	/*
	 * 获取已有的模块数据
	 */
	public function showClass(){
		$cgiclass = D("CgiClass");
		$classdata = $cgiclass->where("parentid=0")->select();
		if (!empty($classdata)){
			$this->assign('classdata',$classdata);
		}
		$this->display('CgiClass:showClass');
	}

	/*
	 * 新增模块页面
	 */

	public function addClassPage(){
		$this->display('CgiClass:addClassPage');
	}

	/*
	 * 新增模块页面
	 */

	public function addClass(){
		$cgiclass = new CgiClassModel();
		$data = $cgiclass->create();
		$classname = $_POST['name'];
		$classdesc = $_POST['description'];
		$owner = $_POST['owner'];
		$this->assign('classname',$classname);
		$this->assign('classdesc',$classdesc);
		$this->assign('owner',$owner);
		if ($data){
			if (!($cgiclass->checkUser($owner))){
				$this->assign('error','当前指定的用户不存在！');
			}else{
				$data['parentid'] = 0;
				$cgiclass->add();
				$this->showClass();//跳转到模块管理列表页
				return ;
			}
		}else{
			$error = $cgiclass->getError();
			$this->assign('error',$error);
		}
		$this->addClassPage();
	}

	/*
	 * 删除模块
	 */
	public function delete(){
		$cgiclass = new CgiClassModel();
		$name = $_GET['name'];
		if (isset($name)){
			$root = $cgiclass->where("name='$name'")->field('id')->find();
			$rootid = $root['id'];
			if (isset($rootid)){
				$cgiclass->deleteClass($rootid);
			}
		}
		$this->showClass();//跳转到模块管理列表页
	}
	
	/*
	 * 编辑模版
	 */
	public function editClass(){
		$cgiclass = new CgiClassModel();
		$name = $_GET['name'];
		if (isset($name)){
			$data = $cgiclass->getByName($name);
			if (!empty($data)){
				$this->assign('classdata',$data);
			}
		}
		$this->display('CgiClass:editClass');		
	}

	/*
	 * 获取cgi树第一层数据
	 */

	public function getChild(){
		$class = D("CgiClass");
		$item = D("CgiItem");
		$id = $_GET['id'];
		if($id >= 0){
			$classes = $class->where("parentid=$id")->select();
			$items = $item->where("classid=$id")->select();
			$child=array();
			if(isset($classes)){
				$child = array_merge($classes,$child);
			}
			if (isset($items)){
				$child = array_merge($items,$child);
			}
			if (!empty($child)){
				$this->assign("child",$child);
				$this->display();
			}
		}
	}
}